package net.atomarrow.bs.service;

import java.util.List;

import net.atomarrow.bean.Pager;
import net.atomarrow.bs.domain.TbAccount;
import net.atomarrow.bs.form.AccountBsForm;
import net.atomarrow.db.parser.Conditions;
import net.atomarrow.sy.base.BaseService;
import net.atomarrow.util.StringUtil;

public class AccountService extends BaseService {

	public List<TbAccount> getList(AccountBsForm form) {
		Conditions conditions = getConditions();
		if (form != null) {
			if (StringUtil.isNotBlank(form.getKeywords())) {
				conditions.parenthesesStart();
				conditions.putLIKE("username", form.getKeywords());
				conditions.setLink(Conditions.OR);
				conditions.putLIKE("nickname", form.getKeywords());
				conditions.setLink(Conditions.OR);
				conditions.putLIKE("phone", form.getKeywords());
				conditions.setLink(Conditions.OR);
				conditions.putLIKE("qq", form.getKeywords());
				conditions.setLink(Conditions.OR);
				conditions.putLIKE("email", form.getKeywords());
				conditions.parenthesesEnd();
			}
		}
		Pager pager = form.getPager();
		if (pager == null) {
			processOrder(form, conditions);
			return this.getList(conditions);
		}
		pager.setDataTotal(this.getCount(conditions));
		processOrder(form, conditions);
		return this.getListByPage(conditions, pager);
	}

	@Override
	protected String getDomainName() {
		return TbAccount.class.getSimpleName();
	}

	
	public void testMax(){
	    System.out.println(getMax(new Conditions(TbAccount.class), "id"));
	}

	public void getAll() {
		List<TbAccount> accounts = getList(getConditions());
		for(TbAccount account:accounts){
			System.out.println(account.getNickname()+":"+account.getUsername());
		}
	}

    public void doTestModify() {
        TbAccount account = getById(TbAccount.class, 1);
        account.setNickname("订单");
        modify(account);
        
    }
	
}
